package com.example.gty.service.Impl;

import com.example.gty.entity.*;
import com.example.gty.mapper.*;
import com.example.gty.service.UsersService;
import com.example.gty.utils.BaseUtil;
import com.example.gty.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private AdminsMapper adminsMapper;
    //登录 并查询用户角色
    public ResultVo login(String no, String password) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("no",no);
        criteria.andEqualTo("password",password);
        List<Users> users = usersMapper.selectByExample(example);
        if (users!=null && !users.isEmpty()){
            //查询该用户角色
            Users user = users.get(0);
            String no1=user.getNo();
            UserRole userRole = userRoleMapper.selectByPrimaryKey(user.getNo());
            if (userRole.getRole().equals("学生")){
                //将学生信息封装 返回
                Student student = studentMapper.selectByPrimaryKey(no1);
                return new ResultVo(1000,"学生",student);
            }else if (userRole.getRole().equals("教师")){
                //将老师信息返回
                Teacher teacher = teacherMapper.selectByPrimaryKey(no1);
                return new ResultVo(1000,"教师",teacher);
            }else {
                //将管理员信息返回
                Admins admins = adminsMapper.selectByPrimaryKey(no1);
                return new ResultVo(1000,"管理员",admins);

            }
        }else{
            return new ResultVo(1001,"登陆失败",null);
        }

    }

    //修改用户个人信息
    public ResultVo updateUsers(String no, BaseUtil object) {
        //查询修改用户的角色
        UserRole userRole = userRoleMapper.selectByPrimaryKey(no);
        //修改学生自身信息
        if (userRole.getRole().equals("学生")){
            String sno = object.getSno();
            String sname = object.getSname();
            String img = object.getImg();
            String sex = object.getSex();
            String cclass = object.getCclass();
            String major = object.getMajor();
            String tel = object.getTel();
            String college = object.getCollege();
            Integer age = object.getAge();

            Student student = new Student(sno, sname, img, sex, cclass, major, tel, college, age, new Date());
            int i = studentMapper.updateByPrimaryKey(student);
            if (i>0){
                System.out.println("学生修改");
                return new ResultVo(1000,"success",null);
            }
            //修改老师自身信息
        }else if (userRole.getRole().equals("教师")){
            String tno = object.getTno();
            String tname = object.getTname();
            String img = object.getImg();
            String sex = object.getSex();
            Integer age = object.getAge();
            String tel = object.getTel();

            Teacher teacher=new Teacher(tno,tname,img,sex,age,tel,new Date());
            int i = teacherMapper.updateByPrimaryKey(teacher);
            if (i>0){
                System.out.println("老师修改");
                return new ResultVo(1000,"success",null);
            }
            //修改管理员自身信息
        }else {
            String ano = object.getAno();
            String aname = object.getAname();
            String sex = object.getSex();
            Integer age = object.getAge();
            String address = object.getAddress();
            String tel = object.getTel();

            Admins admins=new Admins(ano,aname,sex,age,address,tel,new Date());
            System.err.println(admins);
            int i=adminsMapper.updateByPrimaryKey(admins);

            if (i>0){
                System.out.println("管理员修改");
                return new ResultVo(1000,"success",null);
            }
        }
        return new ResultVo(1001,"error1",null);

    }


}
